<?php
if (isset($fileAttachments) && count($fileAttachments)) {
?>
<h3><?php __('Attachments')?></h3>
    <table cellpadding="0" cellspacing="0">
    <?php
        $tableHeaders = $this->Html->tableHeaders(array(
	    '&nbsp;',
            $this->Paginator->sort('title'),
            __('Filename', true),
	    $this->Paginator->sort('mime_type'),
	    $this->Paginator->sort('size'),
            __('Actions', true),
        ));
        echo $tableHeaders;

        $rows = array();
        foreach ($fileAttachments AS $attachment) {
	    $actions = '';
	    	    	    
	    if (isset($this->params['named']['editor'])) {
		$actions .= ' ' . $this->Html->link(__('Insert', true), '#', array(
			'onclick' => "selectURL('file','" . $attachment['Attachment']['filename'] . "');",
			'escape' => false,
		    ));	    	    
	    }	
	    
            /*$actions  = $this->Html->link(__('Edit', true), array(
                'controller' => 'attachments',
                'action' => 'edit',
                $attachment['Attachment']['id'],
            ));*/	    
	    
            $actions .= ' ' . $this->Layout->adminRowActions($attachment['Attachment']['id']);
            $actions .= ' ' . $this->Html->link(__('Delete', true), array(
                'controller' => 'attachments',
                'action' => 'delete',
                $attachment['Attachment']['id'],
                'token' => $this->params['_Token']['key'],
            ), null, __('Are you sure?', true));

            $mimeType = explode('/', $attachment['Attachment']['mime_type']);            
	    $thumbnail = $this->Html->image('/img/icons/page_white.png');    

            $rows[] = array(
		    $thumbnail,
		    $attachment['Attachment']['title'],
		    $this->Html->link($attachment['Attachment']['filename'], '/uploads/files/' . $attachment['Attachment']['filename'], array('target' => '_blank')),
		    $attachment['Attachment']['mime_type'],
		    round($attachment['Attachment']['size'] / 1024, 1) . ' Kib',
		    $actions,		
		);	    	    
        }

        echo $this->Html->tableCells($rows);
        echo $tableHeaders;
    ?>
    </table>

<div class="paging"><?php echo $paginator->numbers(); ?></div>
<div class="counter"><?php echo $paginator->counter(array('format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true))); ?></div>
<?php
} else {
    __('No attachments found');
}
?>